package com.atguigu.wordcount.reducerjoin;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @ClassName ReducerJoinMapper
 * @DeScription ReducerJoinMapper
 * @Author 剧情再美终是戏
 * @Date 2019/11/20 11:36
 * @Version 1.0
 **/

public class ReducerJoinMapper extends Mapper<LongWritable, Text, NullWritable, JoinBean> {

    private String fileName = "";

    private JoinBean joinBean = new JoinBean();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit fileSplit = (FileSplit) context.getInputSplit();
        fileName = fileSplit.getPath().getName();
    }

    // order.txt 1001	01	1
    // pd.txt 01	小米
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] split = value.toString().split("\t");

        if (StringUtils.isNotBlank(fileName)) {
            joinBean.setSource(fileName);
            if ("order.txt".equals(fileName)) {
                joinBean.setOrderId(split[0]);
                joinBean.setpId(split[1]);
                joinBean.setaMount(split[2]);
                joinBean.setpName("");
            } else {
                joinBean.setOrderId("");
                joinBean.setpId(split[0]);
                joinBean.setaMount("");
                joinBean.setpName(split[1]);
            }
            context.write(NullWritable.get(), joinBean);
        }

    }
}
